//
//  WKIdeaViewController.m
//  WeKeTang
//
//  Created by ichina on 2017/8/31.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKIdeaViewController.h"
#import "NSString+KZExtension.h"
#import "WKLoginHandel.h"
#import "WKUniversal.h"
#import "WKNetworkTools.h"

@interface WKIdeaViewController ()<UITextViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextView *TFContent;

@property (weak, nonatomic) IBOutlet UITextField *TFPhoneNum;

@property (weak, nonatomic) IBOutlet UILabel *textNumLabel;

@end

@implementation WKIdeaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navItem.title = @"意见反馈";
    
    [self setupUI];
}

- (void)setupUI{
    
    self.navgationBar.barTintColor = RGBColor(40, 188, 203);
    
    self.statusbar.backgroundColor = RGBColor(40, 188, 203);
    
    [self middleTitleColor:[UIColor whiteColor]];
    
    self.view.backgroundColor = RGBColor(245, 245, 245);

    [self setLeftButtonWithImage:@"icon_return_white" highlightedImage:@"icon_return_white" target:self action:@selector(backAction)];
    
    [self setRightButtonWithTittle:@"提交" color:[UIColor whiteColor] target:self action:@selector(submitAction)];
    
    self.TFContent.text = @"欢迎向我们反映问题或提出建议";
    
    self.TFContent.layer.borderColor = RGBColor(240, 240, 240).CGColor;
    
    self.TFContent.layer.borderWidth = 0.5;
    
    self.TFPhoneNum.layer.borderColor = RGBColor(240, 240, 240).CGColor;
    
    self.TFPhoneNum.layer.borderWidth = 0.5;
    
    self.TFContent.delegate = self;
    
    self.TFPhoneNum.delegate = self;
}
#pragma mark - 提交
- (void)submitAction{
    
    if (StringIsNullOrEmpty(self.TFContent.text)) {
        [self showCustomAlertMessage:@"反馈内容不能为空"];
        return;
    }
    if (StringIsNullOrEmpty(self.TFPhoneNum.text)) {
        [self showCustomAlertMessage:@"请输入联系方式"];
        return;
    }
    if ([self.TFContent.text isEqualToString:@"欢迎向我们反映问题或提出建议"]) {
        [self showCustomAlertMessage:@"请输入问题或建议内容"];
        return;
    }
    if( ![self isPureInt:self.TFPhoneNum.text] || ![self isPureFloat:self.TFPhoneNum.text]){
        [self showCustomAlertMessage:@"请输入正确的联系方式"];
        return;
    }
    __weak typeof(self) weakSelf = self;
    NSString *userid = [WKLoginHandel getLoginDataWithString:@"userid"];
    NSString *sessid = [WKLoginHandel getLoginDataWithString:@"SESSID"];
    NSString *mac = [WKUniversal macaddress];
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:@"suggestion" forKey:@"commandid"];
    [para setObject:userid forKey:@"userid"];
    [para setObject:sessid forKey:@"phpsessid"];
    [para setObject:mac forKey:@"mac"];
    [para setObject:self.TFContent.text forKey:@"content"];
    
    [[WKNetworkTools shareManger] setHttpMethod:@"POST"];
    [[WKNetworkTools shareManger]RunRequest:para path:kServerDomain completionHandler:^(id responseObject) {
        
    } errorHandler:^(NSError *error) {
        
    } finishHandler:^(id responseObject) {
        NSDictionary *dict = [responseObject mj_JSONObject];
        NSLog(@"意见反馈：%@",dict);
        NSDictionary *headDict = dict[@"head"];
        NSString *statuscode = headDict[@"statuscode"];
        NSString *msg = headDict[@"message"];
        if ([statuscode intValue] == 100) {
            
            [weakSelf showCustomAlertMessage:@"提交成功!"];
            self.TFContent.text = @"";
            self.TFPhoneNum.text = @"";
        }else{
            [weakSelf showCustomAlertMessage:msg];
        }
    }];
}
#pragma mark - tool
- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}
//判断是否为浮点形：
- (BOOL)isPureFloat:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return[scan scanFloat:&val] && [scan isAtEnd];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"欢迎向我们反映问题或提出建议"]) {
        textView.text = @"";
    }
}
- (void)textViewDidChange:(UITextView *)textView{
    
    //实时显示字数
    self.textNumLabel.text = [NSString stringWithFormat:@"%lu/300", (unsigned long)textView.text.length];
    //字数限制操作
    if (textView.text.length >= 300) {
        textView.text = [textView.text substringToIndex:300];
        self.textNumLabel.text = @"300/300";
        
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if([text isEqualToString:@"\n"]){
        [self.view endEditing:YES];
    }
    
    return YES;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view resignFirstResponder];
    [self.view endEditing:YES];
    
}
#pragma mark - 返回
- (void)backAction:(UIButton*)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
